package com.example.anni.zhuzhoujia.xiaowan;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.anni.zhuzhoujia.R;

import org.xutils.http.RequestParams;
import org.xutils.x;

/**
 * Created by Anni on 2016/6/21.
 */
public class RevisePasswordActivity extends AppCompatActivity {
    private EditText etOldPassword; //原密码
    private EditText etNewPassword1; //新密码
    private EditText etNewPassword2; //确认新密码

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.revise_password_activity);
        etOldPassword = (EditText) findViewById(R.id.et_old_password);
        etNewPassword1 = (EditText) findViewById(R.id.et_new_password1);
        etNewPassword2 = (EditText) findViewById(R.id.et_new_password2);
        initToolbar();
    }

    private void initToolbar() {
        TextView tvNameTitle;
        tvNameTitle = (TextView) findViewById(R.id.tv_name_title);
        tvNameTitle.setText("修改密码");
    }

    public void clickRevisePassword(View v) {
        RequestParams params =
                new RequestParams("http://tr.zzapi.gson.cn/api/user/change/password");
        //旧密码
        String oldPassword = etOldPassword.getText().toString();
        //新密码
        final String newPassword1 = etNewPassword1.getText().toString();
        //确认新密码
        final String newPassword2 = etNewPassword2.getText().toString();

        params.addBodyParameter("oldPassword", oldPassword);

        if (newPassword1.equals(newPassword2)) {
            params.addBodyParameter("password", newPassword1);
            x.http().post(params, new CallBackAdapter<String>() {
                @Override
                public void onSuccess(String s) {
                    //Toast.makeText(RevisePasswordActivity.this, s, Toast.LENGTH_LONG).show();
                    JSONObject object = JSON.parseObject(s);
                    if (object.getBoolean("success")) {
                        Toast.makeText(RevisePasswordActivity.this, "修改成功", Toast.LENGTH_LONG).show();
                        finish();
                    } else {
                        Toast.makeText(RevisePasswordActivity.this, object.getString("message"), Toast.LENGTH_LONG).show();
                    }
                }
            });
        } else {
            Toast.makeText(RevisePasswordActivity.this, "输入密码不一致", Toast.LENGTH_LONG).show();
        }
    }
}
